using System;
using UnityEngine;
using System.Collections;


public class WanderBehaviour : SteeringBehaviour
{
	float wanderOffset = 100f;
	float wanderRadius = 150f;
	float needNewWanderRange = 30f;
	
	public WanderBehaviour ()
	{
	}
	
	public override Vector3 getSteering (AgentBehaviourScript agent)
	{
		Vector3 agentPos = agent.getPosition();
		Vector3 agentCurTargetPos = agent.getDestination();
		
		if((agentPos - agentCurTargetPos).magnitude < needNewWanderRange)
		{
			float agentOrientation = agent.rigidbody.rotation.eulerAngles.y;
			Vector3 wanderTargetPosition = new Vector3(0,agent.getPosition().y,0);
			double randomWanderAngle =  (UnityEngine.Random.value)*360; //gens a number between 0 and 360
			
			//sets the target, in the direction that the agent is facing, 'wanderDistance' away from the agent
			wanderTargetPosition.x = agentPos.x + wanderOffset * (float)Math.Sin( (Math.PI/180) * agentOrientation);
			wanderTargetPosition.z = agentPos.z + wanderOffset * (float)Math.Cos( (Math.PI/180) * agentOrientation);
			
			//moves the target to a random point, at the radius of the wander target
			wanderTargetPosition.x += wanderRadius * (float)Math.Sin( (Math.PI/180) * randomWanderAngle);
			wanderTargetPosition.z += wanderRadius * (float)Math.Cos( (Math.PI/180) * randomWanderAngle);
			
			return wanderTargetPosition;
		}
		return agentCurTargetPos;
	}
}


